# coding=utf-8
# author=haishan09@gmail.com
from collections import defaultdict


class ProviderAlreadyRegistered(Exception):
    pass


class ProviderNotRegistered(Exception):
    pass


class ProviderManager(object):

    def __init__(self):
        self._registry = defaultdict(lambda :defaultdict(lambda: None))

    def register(self, config_module, config_type, config_cls):
        if self._registry[config_module][config_type]:
            raise ProviderAlreadyRegistered(
                'The {0}\' config is already registered'.format(config_type))
        self._registry[config_module][config_type] = config_cls

    def unregister(self, config_module, config_type):
        if not self._registry[config_module][config_type]:
            raise ProviderNotRegistered(
                'The {0}\' config is not registered'.format(config_type))
        self._registry[config_module][config_type] = None

    def get_provider(self, config_module, config_type):
        return self._registry[config_module][config_type]


provider_manager = ProviderManager()

def provider_register(module, provider_type):
    def _provider_register_wrapper(provider_cls):
        provider_manager.register(module, provider_type, provider_cls)
        return provider_cls
    return _provider_register_wrapper

